﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YRevitJig
{
	public class XCurvesFlicker : XUiJigBase
	{
		public XCurvesFlicker(UIDocument uiDoc) : base(uiDoc)
		{
			this.m_lstCvs = new List<Curve>();
			this.m_thread = new Thread(new ParameterizedThreadStart(XCurvesFlicker.StartFlicker));
			this.IsThreadComplete = false;
			this.FlickerTime = 500;
		}

		private void Start()
		{
			try
			{
				this.m_thread.Start(this);
			}
			catch
			{
			}
		}

		private void Stop()
		{
			try
			{
				this.m_thread.Join(1000);
				this.m_thread.Abort();
			}
			catch
			{
			}
		}

		private static void StartFlicker(object obj)
		{
			try
			{
				XCurvesFlicker xcurvesFlicker = obj as XCurvesFlicker;
				while (!xcurvesFlicker.IsThreadComplete)
				{
					xcurvesFlicker.DoFlicker();
					Thread.Sleep(xcurvesFlicker.FlickerTime);
				}
			}
			catch
			{
			}
		}

		private void DoFlicker()
		{
			bool flag = !this.m_thread.IsAlive;
			if (!flag)
			{
				List<XDrawable2D> list = new List<XDrawable2D>();
				foreach (Curve curve in this.Curves)
				{
					Line line = curve as Line;
					bool flag2 = line != null;
					if (flag2)
					{
						XYZ endPoint = line.GetEndPoint(0);
						XYZ endPoint2 = line.GetEndPoint(1);
                        System.Drawing.Point ptStart = base.Revit2Client(endPoint);
                        System.Drawing.Point ptEnd = base.Revit2Client(endPoint2);
						XLine2D item = new XLine2D(ptStart, ptEnd);
						list.Add(item);
					}
					else
					{
						Arc arc = curve as Arc;
						bool flag3 = arc != null && arc.IsBound;
						if (flag3)
						{
							XYZ endPoint3 = arc.GetEndPoint(0);
							XYZ endPoint4 = arc.GetEndPoint(1);
							XYZ point = arc.Evaluate(0.5, true);
                            System.Drawing.Point ptStart2 = base.Revit2Client(endPoint3);
                            System.Drawing.Point ptEnd2 = base.Revit2Client(endPoint4);
                            System.Drawing.Point ptOnArc = base.Revit2Client(point);
							XArc2D item2 = new XArc2D(ptStart2, ptEnd2, ptOnArc);
							list.Add(item2);
						}
						else
						{
							bool flag4 = arc != null && !arc.IsBound;
							if (flag4)
							{
								XYZ center = arc.Center;
								XYZ point2 = center + base.UiDoc.ActiveView.RightDirection * arc.Radius;
                                System.Drawing.Point point3 = base.Revit2Client(center);
                                System.Drawing.Point pt = base.Revit2Client(point2);
								int distance = XDrawable2D.GetDistance(pt, point3);
								XCircle2D item3 = new XCircle2D(point3, distance);
								list.Add(item3);
							}
						}
					}
				}
			}
		}

		public XUiJigBase.EJigResult DoJig()
		{
			try
			{
				this.Start();
				XYZ xyz = base.UiDoc.Selection.PickPoint("按任意键退出:");
			}
			catch
			{
				return XUiJigBase.EJigResult.eRtCancel;
			}
			finally
			{
				this.IsThreadComplete = true;
			}
			return XUiJigBase.EJigResult.eRtOk;
		}

        public System.Drawing.Color PenColor
		{
			set
			{
				this.m_PenColor = value;
			}
		}

		public int PenWidth
		{
			set
			{
				this.PenWidth = value;
			}
		}

		public int FlickerTime { get; set; }

		public List<Curve> Curves
		{
			get
			{
				return this.m_lstCvs;
			}
		}

		public bool IsThreadComplete { get; set; }

		private List<Curve> m_lstCvs = null;

		private Thread m_thread = null;

        private System.Drawing.Color m_PenColor = System.Drawing.Color.Red;
	}
}
